﻿using AForge.Video.DirectShow;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TestProject
{
    public partial class frmCamera : UIPage
    {
        /// <summary>
        /// 所有摄像设备
        /// </summary>
        private FilterInfoCollection videoDevices;
        /// <summary>
        /// 摄像头设备信息
        /// </summary>
        private VideoCaptureDevice videoDevice;
        /// <summary>
        /// 摄像头分辨率
        /// </summary>
        private VideoCapabilities[] videoCapabilities;

        public frmCamera()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 窗体加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmCamera_Load(object sender, EventArgs e)
        {
            try
            {
                //获取所有摄像设备
                videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
                if (videoDevices.Count != 0)
                {
                    //循环遍历 
                    foreach (FilterInfo device in videoDevices)
                    {
                        //下拉框赋值
                        cmb_Camera.Items.Add(device.Name);

                        labtxt.Text += string.Format("{0}设备已加载完毕...", device.Name);
                    }

                    //控制按钮状态
                    EnableControlStatus(false);
                }
                else
                {
                    cmb_Camera.Items.Add("没有找到摄像头");
                }

                //默认取第一个
                cmb_Camera.SelectedIndex = 0;
            }
            catch (Exception ex)
            {
                ShowErrorDialog("窗体加载异常:" + ex.Message);
            }
        }

        /// <summary>
        /// 选择设备
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmb_Camera_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                //判断设备数量
                if (videoDevices.Count != 0)
                {
                    videoDevice = new VideoCaptureDevice(videoDevices[cmb_Camera.SelectedIndex].MonikerString);
                    //获取该设备的分辨率
                    GetDeviceResolution(videoDevice);
                }
            }
            catch (Exception ex)
            {
                ShowErrorDialog("选择设备异常:" + ex.Message);
            }
        }

        /// <summary>
        /// 获取设备分辨率
        /// </summary>
        /// <param name="videoCaptureDevice"></param>
        private void GetDeviceResolution(VideoCaptureDevice videoCaptureDevice)
        {
            try
            {
                cmb_Resolution.Items.Clear();
                videoCapabilities = videoCaptureDevice.VideoCapabilities;
                //循环遍历
                foreach (VideoCapabilities capabilty in videoCapabilities)
                {
                    //下拉框赋值
                    cmb_Resolution.Items.Add($"{capabilty.FrameSize.Width} x {capabilty.FrameSize.Height}");
                }
                //默认取第一个
                cmb_Resolution.SelectedIndex = 0;
            }
            catch (Exception ex)
            {
                ShowErrorDialog("设备分辨率异常:" + ex.Message);
            }
        }

        /// <summary>
        /// 打开摄像头
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Open_Click(object sender, EventArgs e)
        {
            try
            {
                if (videoDevice != null)
                {
                    if ((videoCapabilities != null) && (videoCapabilities.Length != 0))
                    {
                        videoDevice.VideoResolution = videoCapabilities[cmb_Resolution.SelectedIndex];

                        vispShoot.VideoSource = videoDevice;
                        vispShoot.Start();
                        EnableControlStatus(true);

                        labtxt.Text = "设备打开...";
                    }
                }
            }
            catch (Exception ex)
            {
                ShowErrorDialog("打开设备异常:" + ex.Message);
            }
        }

        /// <summary>
        /// 关闭摄像头
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Close_Click(object sender, EventArgs e)
        {
            try
            {
                DisConnect();
                EnableControlStatus(false);
                pictureBox1.Image = null;

                labtxt.Text = "设备关闭...";
            }
            catch (Exception ex)
            {
                ShowErrorDialog("关闭设备异常:" + ex.Message);
            }
        }

        /// <summary>
        /// 拍照
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_takephoto_Click(object sender, EventArgs e)
        {
            try
            {
                Bitmap img = vispShoot.GetCurrentVideoFrame();
                pictureBox1.Image = img;

                //加载的图片自适应picrurebox大小
                pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
                labtxt.Text = "拍照完毕";

                var res = ShowAskDialog("是否需要保存至本地?");
                if (!res)
                {
                    labtxt.Text = "不保存";
                }
                else
                {
                    #region 配置文件内容
                    saveFileDialog1.Filter = @"所有图像文件|*.bmp;*.pcx;*.png;*.jpg;*.gif;" +
                                            "*.tif;*.ico;*.dxf;*.cgm;*.cdr;*.wmf;*.eps;*.emf|" +
                                            "位图(*.bmp;*.jpg;*.png;...)|*.bmp;*.pcx;*.png;*.jpg;*.gif;*.tif;*.ico|" +
                                            "矢量图(*.wmf;*.eps;*.emf;..)|*.dxf;*.cgm;*.cdr;*.wmf;*.eps;*.emf"; ;//设置文件类型
                    saveFileDialog1.FileName = "图片文件";//文件名
                    if (saveFileDialog1.ShowDialog() != DialogResult.OK)
                    {
                        return;
                    }
                    #endregion
                    //最终压缩后要保存的路径
                    string saveFilePath = this.saveFileDialog1.FileName;
                    //保存图片
                    img.Save(saveFilePath, ImageFormat.Png);

                    labtxt.Text = "已保存";
                }
            }
            catch (Exception ex)
            {
                ShowErrorDialog("拍照异常:" + ex.Message);
            }
        }

        /// <summary>
        /// 断开连接
        /// </summary>
        private void DisConnect()
        {
            try
            {
                if (vispShoot.VideoSource != null)
                {
                    vispShoot.SignalToStop();
                    vispShoot.WaitForStop();
                    vispShoot.VideoSource = null;
                }
            }
            catch (Exception ex)
            {
                ShowErrorDialog("断开连接异常:" + ex.Message);
            }
        }

        /// <summary>
        /// 控制按钮状态
        /// </summary>
        /// <param name="status"></param>
        private void EnableControlStatus(bool status)
        {
            try
            {
                cmb_Camera.Enabled = !status;//设备列表
                cmb_Resolution.Enabled = !status;//分辨率列表
                btn_Open.Enabled = !status;//打开按钮
                btn_Close.Enabled = status;//关闭按钮
                btn_takephoto.Enabled = status;//拍照按钮
                btn_Running.Enabled = status;//连拍按钮
                btn_StopRun.Enabled = status;//停止连拍
                btn_Record.Enabled = status;//开始录制按钮
                btn_BreakRecord.Enabled = status;//暂停录制按钮
                btn_StopRecord.Enabled = status;//停止录制按钮
            }
            catch (Exception ex)
            {
                ShowErrorDialog("按钮状态异常:" + ex.Message);
            }
        }

        /// <summary>
        /// 连拍
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Running_Click(object sender, EventArgs e)
        {
            ShowWarningTip("暂未实现");
        }

        /// <summary>
        /// 停止连拍
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_StopRun_Click(object sender, EventArgs e)
        {
            ShowWarningTip("暂未实现");
        }

        /// <summary>
        /// 开始录制
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Record_Click(object sender, EventArgs e)
        {
            ShowWarningTip("暂未实现");
        }

        /// <summary>
        /// 暂停录制
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_BreakRecord_Click(object sender, EventArgs e)
        {
            ShowWarningTip("暂未实现");
        }

        /// <summary>
        /// 停止录制
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_StopRecord_Click(object sender, EventArgs e)
        {
            ShowWarningTip("暂未实现");
        }
    }
}
